package com.hongdee.scm.util;

import com.hongdee.common.util.TokenUtil;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2017/7/19.
 */
public class SerialNumFactory {
    private static class SerialNumBuiler {
        private static final Map<String, Long> serialMap = new HashMap<>();
        private static final SimpleDateFormat sdf  = new SimpleDateFormat("yyyyMMdd");

        private String getSerialNum(String uid, String type){
            if (null == uid){
                return "0";}
            if (null == type){
                type="_A";}

            String cId = TokenUtil.getCompanyId(uid);
            String key = String.format("%s%s%s", cId, sdf.format(Calendar.getInstance().getTime()), type);
            long sn = 1;
            if ( serialMap.containsKey(key))
            {
                sn = serialMap.get(key) + 1;
            }
            else {
                sn = Long.valueOf(sdf.format(Calendar.getInstance().getTime()))*1000+sn;
            }
            serialMap.put(key, sn);
            return ""+sn;
        }
    }

   static  SerialNumBuiler serialNumBuiler = new SerialNumBuiler();
    public  static String generateSerialNum(String uid, String type)
    {
        return serialNumBuiler.getSerialNum(uid, type);
    }

}

